#!/usr/bin/env python
# coding: utf-8


import json
import requests

GITTE_REPO = "oec-application"
DESCIRBE_PATH = './describe'
ISSUE_TYPE = "HDC-任务打榜赛"
ASSIGNEE = "superninesun"   #负责人个人空间地址

class IssueMessage(object):
    def __init__(self):
        self.repo = GITTE_REPO
        self.title = ""
        self.describe = ""
        self.issue_type = ISSUE_TYPE
        self.assignee = ASSIGNEE

    def get_issue_describe(self):
        file = open(DESCIRBE_PATH)
        self.describe = file.read()
        file.close()

    def set_issue_title(self, title):
        self.title = title


class AutoIssue(object):

    def __init__(self, access_token, issuemessage: IssueMessage):
        self.header = {"Content-Type": "application/json"}
        self.access_token = access_token
        self.ip = "gitee.com"
        self.owner = "src-openEuler"
        self.body_temp = {"access_token": self.access_token}
        self.issuemessage = issuemessage

    def get_request(self, url):  # get https ruquest
        requests.packages.urllib3.disable_warnings()
        url = "https://" + self.ip + url
        req_res = requests.get(url, verify=False)
        return json.loads(req_res.text)

    def post_request(self, url, body):
        requests.packages.urllib3.disable_warnings()
        url = "https://" + self.ip + url
        body = json.dumps(body)
        req_res = requests.post(url, data=body, verify=False, headers=self.header)
        return json.loads(req_res.text)

    def gen_issue_body(self):
        self.issuemessage.get_issue_describe()
        body = self.body_temp
        body['repo'] = self.issuemessage.repo
        body['title'] = self.issuemessage.title
        body['body'] = self.issuemessage.describe
        body['issue_type'] = self.issuemessage.issue_type
        body['assignee'] = self.issuemessage.assignee

        return body

    def create_issue(self):
        body = self.gen_issue_body()
        url = "/api/v5/repos/%s/issues" % self.owner
        print(self.post_request(url, body))


if __name__ == "__main__":
    issue_message = IssueMessage()
    issue_message.set_issue_title("這是一個測試，不要领取任务")
    issue = AutoIssue("da38fca3df8722afac3ecb7b125fe802", issue_message)
    issue.create_issue()
